package com.example.common;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class ResponseResult {

    /**
     * 状态码
     */
    private int code;
    /**
     * 业务数据
     */
    private Object data;
    /**
     * 状态描述
     */
    private String message;

    /**
     * 附带集
     */
    private Object properties;

    public ResponseResult() {
        this(null, State.SUCCESS);
    }

    public ResponseResult(Object data) {
        this(data, State.SUCCESS);
    }

    public ResponseResult(Object data, State state) {
        this.code = state.code;
        this.data = dataToDict(data);
        this.message = state.message;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = dataToDict(data);
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }


    public Object getProperties() {
        return properties;
    }

    public void setProperties(Object properties) {
        this.properties = properties;
    }

    @Override
    public String toString() {
        return "JResult [code=" + code + ", data=" + data + ", message=" + message + ", properties=" + properties + "]";
    }

    public Object dataToDict(Object data) {
        if (data != null && data instanceof Collection) {
            Map<String, Object> map = new HashMap<String, Object>(2);
            map.put("list", data);
            data = map;
        }
        return data;
    }

    public void putState(State state) {
        this.code = state.code;
        this.message = state.message;
    }

    public static ResponseResult createErrorResult(int code, String message) {
        ResponseResult jResult = new ResponseResult();
        jResult.setCode(code);
        jResult.setMessage(message);
        return jResult;
    }

    public static ResponseResult createErrorResult(State state) {
        ResponseResult jResult = new ResponseResult();
        jResult.setCode(state.code);
        jResult.setMessage(state.message);
        return jResult;
    }

    public static ResponseResult createErrorResult(State state, Object data) {
        ResponseResult jResult = new ResponseResult(data);
        jResult.setCode(state.code);
        jResult.setMessage(state.message);
        return jResult;
    }

    public static ResponseResult createErrorResult(int code, String message, Object data) {
        ResponseResult jResult = new ResponseResult(data);
        jResult.setCode(code);
        jResult.setMessage(message);
        return jResult;
    }

    /**
     * 解析方法
     * to json
     *
     * @return
     */
    public JSONObject toJSONObject() {
        return (JSONObject) JSON.toJSON(data);
    }

    /**
     * 解析方法
     * to json
     *
     * @return
     */
    public JSONArray toJSONArray() {
        return (JSONArray) JSON.toJSON(data);
    }

    /**
     * 解析方法
     * to map
     *
     * @return
     */
    @SuppressWarnings("unchecked")
    public Map<String, Object> toMap() {
        return (Map<String, Object>) JSON.toJSON(data);
    }


    /**
     * 状态码枚举
     *
     * @author yangwei
     */
    public enum State {
        /**
         * 成功类型
         */
        SUCCESS(200, "成功！"),
        NOT_LOGIN(203, "未登录！"),
        PASSWORD_ACCOUNT_ERROR(1000,"账号或密码错误"),
        VERIFICATION_ERROR(1007,"验证码错误"),

        USER_EXIST(1008,"用户已存在"),
        PASSWORD_NOT(1010,"旧密码错误"),
        NUM_EXIT(1014,"编号已存在"),
        OPERATE_FAIL(1019,"操作失败"),
        PHONE_ERROR(1021,"修改失败,手机号或者账号错误,请重新输入!"),
        CROPS_NOT_EXIST(1022,"商品不存在"),
        BALANCE_NOT(1023,"余额不足，请充值"),
        STOCK_NOT(1024,"库存不足")
        ;


        private int code;
        private String message;

        State(int code, String message) {
            this.code = code;
            this.message = message;
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }
}
